import { ref, onMounted } from "vue"
//  一定不能没有这一步
import type { Ref } from "vue"
import type { orderListType } from "@/typings/orderType"
import { getOrderApi } from "@/api/orderApi"

//  元组
type orderlistType = [Ref<orderListType[]>, Ref<number>, (page: number) => Promise<void>]

export const useOrderList = (pageNum = 1):orderlistType  => {
    const data = ref<orderListType[]>([])
    const total = ref(0)               

    //  获取可选参数来得到对应的订单数据，
    const loadData = async (page: number) => {
        let ret = await getOrderApi(page)
        if (ret.code === 0) {
            data.value = ret.data.users
            total.value = ret.data.total
        }
    }

    onMounted(() => {
        loadData(pageNum)
    })

    return [data, total, loadData]
}

export default useOrderList